
package com.jf.cloud.live.controller.app;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.live.dto.LiveRoomDTO;
import com.jf.cloud.live.model.LiveRoom;
import com.jf.cloud.live.service.LiveRoomService;
import com.jf.cloud.live.vo.LiveRoomVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 直播间
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("appLiveRoomController")
@RequestMapping("/ma/live_room")
@Tag(name = "直播间")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取直播间列表" , description = "分页获取直播间列表")
    public ServerResponseEntity<PageVO<LiveRoom>> page(@Valid PageDTO pageDTO, @ParameterObject LiveRoom liveRoom) {
        liveRoom.setShopId(null);
        return ServerResponseEntity.success(liveRoomService.page(pageDTO, liveRoom));
    }

    @GetMapping
    @Operation(summary = "获取直播间" , description = "根据id获取直播间")
    public ServerResponseEntity<LiveRoom> getById(@ParameterObject @RequestParam Long id) {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if(Objects.isNull(userInfoInTokenBO)){
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(liveRoomService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存直播间" , description = "保存直播间")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveRoomDTO liveRoomDTO) {
        LiveRoom liveRoom = mapperFacade.map(liveRoomDTO, LiveRoom.class);
        liveRoomService.save(liveRoom);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新直播间" , description = "更新直播间")
    public ServerResponseEntity<Void> update(@Valid @RequestBody LiveRoomDTO liveRoomDTO) {
        LiveRoom liveRoom = mapperFacade.map(liveRoomDTO, LiveRoom.class);
        liveRoomService.update(liveRoom);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播间" , description = "根据直播间id删除直播间")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        liveRoomService.deleteById(id);
        return ServerResponseEntity.success();
    }

    @GetMapping("/pageTopRoom" )
    @Operation(summary = "置顶直播间列表信息" , description = "置顶直播间列表信息")
    public ServerResponseEntity<PageVO<LiveRoomVO>> getLiveRoomPageByTop(@Valid PageDTO pageDTO){
        LiveRoomDTO liveRoom = new LiveRoomDTO();
        liveRoom.setRoomTop(1);
        liveRoom.setSearchType(1);
        liveRoom.setName("");
        return ServerResponseEntity.success(liveRoomService.pageRoomAndDetail(pageDTO,liveRoom));
    }
}
